<template>
	<view class="bigBox">
		<!-- <u-navbar :fixed="true" :placeholder="true" :title="pointName" @leftClick="leftClick" bgColor="#0F53AC"
			leftIconColor="#fff">
		</u-navbar> -->
		<view class="bigTitle">
			<view class="img">
				<image class="title-img" src="../../../static/images/index/xcsvg.png"></image>
			</view>
			<view class="text">
				{{pointName}}
			</view>
		</view>
		<view class="line"></view>
		<view class="content" v-for="(item,index) in measureList" :key="index" @click.stop="handleToCheck(item,index)">
			<view class="item-bottom">
				{{`${index + 1}、` + item.measure}}
			</view>
			<view class="itemBtn">
				<view class="no-danger" @click.stop="handleToCheck(item,index)"
					v-if="item.remark == '无隐患' && item.dangerFlag == 0">
					无隐患
				</view>
				<u-badge @click.stop="handleToCheck(item,index)" v-else bgColor="red" max="99" :value="item.remark">
				</u-badge>
				<u-icon name="arrow-right"></u-icon>
			</view>
			<!-- 逻辑更改 -->
			<!-- <view class="item-radio">
				<u-radio-group v-model="item.dangerFlag" :disabled="isInspec == 1">
					<u-radio shape="circle" label="无隐患" :name="0" activeColor="#0F53AC" size="22" labelSize="15">
					</u-radio>
					<u-radio shape="circle" label="有隐患" :name="1" activeColor="#0F53AC" size="22" labelSize="15"
						:customStyle="{marginLeft: '100rpx'}"></u-radio>
				</u-radio-group>
			</view>

			<view v-if="item.dangerFlag===0" class="kiss-upDown-list" style="border: 0;">
				<view class="input-textrea" style="margin: 0;" v-if="isInspec == 0">
					<u--textarea v-model="item.remark" placeholder="请输入备注" maxlength="1000">
					</u--textarea>
				</view>
				<view class="textInput" v-else>备注信息：{{item.remark || '无'}}</view>
			</view>

			<view v-if="item.dangerFlag==1" class="kiss-list">
				<view class="kiss-list-title" style="padding: 32rpx 0 0 0;border: 0;" @click="handleToCheck(item)">
					<view class="left">
						<view style="color: red;" v-if="isInspec == 0">*</view>异常信息{{isInspec == 0?'填写':''}}
					</view>
					<view class="right1">
						<view class="my-num">
							<view v-if="item.dangerNum" class="num">{{item.dangerNum}}</view>
							{{isInspec == 0?'去填写':'查看详情'}}
						</view>
						<view class="iconfont icon-app-xiangyou"></view>
					</view>
				</view>
			</view> -->
		</view>
		<view class="kiss-list">
			<view class="kiss-list-title" style="padding: 22rpx 32rpx;">
				<view class="left">
					管控巡查人
				</view>
				<view class="right1" v-if="isInspec == 0">
					<view v-if="isSign">
						<view class="my-btn2" @click="handleSeeSign" v-if="sign">查看签名</view>
						<view class="my-btn" @click="handleCheckSign" v-else>一键签名</view>
					</view>
					<template v-else>
						<view @click="handleToSetSign">请签字</view>
						<view class="iconfont icon-app-xiangyou"></view>
					</template>

				</view>
				<view class="signBox" v-else>
					<view class="text">{{inspectionTask.userName}}</view>
					<view class="my-btn2" @click="handleSeeSign">查看签名</view>
				</view>
			</view>
		</view>
		<view class="kiss-list" v-if="isInspec == 1">
			<view class="kiss-list-title">
				<view class="left">
					管控提交时间
				</view>
				<view class="right1">
					{{inspectionTask.submitTime || '无'}}
				</view>

			</view>
		</view>
		<template v-if="isInspec == 0">
			<view class="xing-subside"></view>
			<view class="xing-bottom">
				<view class="btn-right" @click="$dianji(sumbit,'onoff1')">提交</view>
			</view>
		</template>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				onoff: true,
				onoff1: true,
				taskId: '', //巡查任务id,
				planName: '管控项目', //巡查任务名称/检查项目
				pointName: '', //风险点名称
				unitName: '', //风险单元名称
				measureList: [], //检查规则
				inspectionTask: {}, //巡查任务
				// 是否设置过签名
				isSign: false,
				oldSign: '',
				// 签名设置
				sign: '',
				openValue: '0',
				opIndex: '',
				isInspec: 0, //0编辑 1查看详情
			}
		},
		onLoad(options) {
			this.taskId = options.taskId
			this.isInspec = options.isInspec
			this.pointName = options.pointName
			this.unitName = options.unitName
			this.planName = options.planName
			this.initData()
			this.getSign()
		},
		onShow() {
			var pages = getCurrentPages(); // 获取当前页面栈
			var currentPage = pages[pages.length - 1]; // 当前页面
			if (currentPage.data.refreshIfNeeded) {
				currentPage.data.refreshIfNeeded = false;
				this.getSign(true)
			}
			if (currentPage.data.needGetData) {
				currentPage.data.needGetData = false;
				this.initData()
			}
		},
		onUnload() {
			var pages = getCurrentPages(); //当前页面栈
			if (pages.length > 1) {
				var beforePage = pages[pages.length - 2]; //获取上一个页面实例对象
				beforePage.data.needNotData = true;
			}
		},
		methods: {
			leftClick() {
				let that = this
				if (this.isInspec == 1) {
					uni.navigateBack({
						delta: 1
					})
					return
				}

				uni.showModal({
					title: '提示',
					content: '是否保存当前内容？',
					confirmText: '保存',
					cancelText: '不保存',
					success: function(res) {
						if (res.confirm) {
							that.draft()
						} else {
							uni.navigateBack({
								delta: 1
							})
						}
					}
				});
			},
			getSign(type = false) {
				uni.$u.http.get(`/base/mobile/userInfo/getLoginUserInfo`).then(res => {
					if (res.code == 0) {
						if (res.data.userSign) {
							this.isSign = true
							this.oldSign = res.data.userSign
							if (type) {
								this.sign = res.data.userSign
							}
						}
					}
				}).catch(err => {})
			},
			initData() {
				uni.$u.http.get(`/check/mobile/riskControlMeasure/getControlMeasureList/${this.taskId}`).then(res => {
					if (res.code == 0) {
						this.measureList = res.data.measureList;
						this.inspectionTask = res.data.inspectionTask;
						this.openValue = this.opIndex
						if (res.data.inspectionTask.sign) {
							this.isSign = true
							this.sign = res.data.inspectionTask.sign
						}
					}
				}).catch(err => {})
			},
			draft() {
				let arr = []
				this.measureList.map(item => {
					let obj = {
						id: item.situationId,
						measureId: item.id,
						dangerFlag: item.dangerFlag,
						remark: item.remark
					}
					arr.push(obj)
				})
				const params = {
					id: this.inspectionTask.id,
					inspectionStatus: 1,
					sign: this.sign, //签名照片
					situationList: arr
				}
				uni.$u.http.post(`/check/mobile/riskInspectionTask/saveInspectionTask`, params)
					.then(res => {
						if (res.code == 0) {
							uni.$u.toast('提交成功');
							uni.navigateBack({
								delta: 1
							})
						}
					})
			},
			sumbit() {
				let that = this
				if (this.sign == '') {
					uni.$u.toast('请先签名')
					return
				}
				let flag = false
				let arr = []
				this.measureList.map(item => {
					if (item.dangerFlag === '') {
						flag = true
					}
				})
				if (flag) {
					uni.showModal({
						title: '提示',
						content: '确定提交巡查信息吗？未巡查项默认【无隐患】',
						confirmText: '确定',
						cancelText: '取消',
						success: function(res) {
							if (res.confirm) {

								const params = {
									id: that.inspectionTask.id,
									sign: that.sign, //签名照片
								}

								uni.$u.http.post(`/check/mobile/riskInspectionTask/saveInspectionTask`, params)
									.then(res => {
										if (res.code == 0) {
											uni.$u.toast('提交成功');
											uni.navigateBack({
												delta: 1
											})
										}
									})
							}
						}
					});

					return
				}

				const params = {
					id: this.inspectionTask.id,
					sign: this.sign, //签名照片
				}
				uni.showModal({
					title: '提示',
					content: '确定提交巡查信息吗？提交后将不可修改',
					confirmText: '确定',
					cancelText: '取消',
					success: function(res) {
						if (res.confirm) {
							uni.$u.http.post(`/check/mobile/riskInspectionTask/saveInspectionTask`, params)
								.then(res => {
									if (res.code == 0) {
										uni.$u.toast('提交成功');
										uni.navigateBack({
											delta: 1
										})
									}
								})
						}
					}
				});
			},
			handleToCheck(it) {
				if (this.isInspec == 1) {
					if (it.remark == "无隐患" || it.remark == '') {
						// 无隐患
						uni.navigateTo({
							url: `/pages/subPackagesA/troble/hideRectified?id=${it.dangerId}&remark=${it.remark}`
						})
					} else {
						// 有隐患 多个隐患列表
						uni.navigateTo({
							url: `/pages/subPackagesA/troble/multipleTroble?isRiks=${true}&type=${0}&ruleId=${it.id}&inspectionId=${this.inspectionTask.id}&content=${it.measure}&isShow=${false}&pointName=${this.pointName}&unitName=${this.unitName}&isInspec=${1}&suggestion=${it.suggestion}`
						})
					}
				} else {
					if (it.remark == "无隐患") {
						// 无隐患
						uni.navigateTo({
							url: `/pages/subPackagesA/troble/hideRectified?id=${it.dangerId}&remark=${it.remark}&isShowTime=${2}`
						})

					} else if (it.remark == '') {
						// 未填写 
						uni.navigateTo({
							url: `/pages/subPackagesA/troble/checkTroble?isRiks=${true}&ruleId=${it.id}&inspectionId=${this.inspectionTask.id}&content=${it.measure}&isShow=${false}&pointName=${this.pointName}&unitName=${this.unitName}&isInspec=${0}&remark=${it.remark}&dangerId=${it.dangerId}`
						})
					} else {
						// 有隐患 多个隐患列表

						uni.navigateTo({
							url: `/pages/subPackagesA/troble/multipleTroble?isRiks=${true}&type=${0}&ruleId=${it.id}&inspectionId=${this.inspectionTask.id}&content=${it.measure}&isShow=${true}&isLook=${0}&pointName=${this.pointName}&unitName=${this.unitName}&isInspec=${0}&planName=${this.planName}`
						})
					}
				}



			},
			handleCheckSign() {
				this.sign = this.oldSign
			},
			handleSeeSign() {
				let sign = ''
				if (this.isInspec == 1) {
					sign = this.inspectionTask.sign
				} else {
					sign = this.sign
				}
				uni.$u.http.get(
					`/base/upload/bindFileUrl?fileUrl=${sign}`
				).then(res => {
					if (res.code == 0) {
						uni.navigateTo({
							url: `/pages/subPackagesD/mycenter/sginselect?url=
								${encodeURIComponent(JSON.stringify(res.data))}&noShow=true`
						})
					}
				}).catch(err => {})
			},
			handleToSetSign() {
				uni.navigateTo({
					url: `/pages/subPackagesD/mycenter/signname?nameType=sign`
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.bigBox {

		.bigTitle {
			font-size: 34rpx;
			font-weight: 500;
			background-color: #fff;
			padding: 32rpx;
			color: #2B2B2B;
			display: flex;
			align-items: center;

			.img {
				width: 40rpx;
				margin-right: 6rpx;
				display: flex;
				align-items: center;

				.title-img {
					width: 40rpx;
					height: 40rpx;
					object-fit: cover;

				}
			}

			.text {
				word-break: break-all;
				word-wrap: break-word;
			}
		}

		.line {
			height: 16rpx;
			background-color: #F5F5F5;
		}

		.my-btn {
			padding: 12rpx 24rpx;
			background: rgba(255, 255, 255, 1);
			border: 1px solid rgba(15, 83, 172, 1);
			color: rgba(15, 83, 172, 1);
			font-size: 28rpx;
			border-radius: 8rpx;
		}

		.my-btn2 {
			padding: 12rpx 24rpx;
			background: rgba(15, 83, 172, 1);
			border: 1px solid rgba(15, 83, 172, 1);
			color: rgba(255, 255, 255, 1);
			font-size: 28rpx;
			border-radius: 8rpx;
		}

		.content {
			padding: 32rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #F5F5F5;

			.item-bottom {
				flex: 1;
				line-height: 48rpx;
				font-size: 16px;
				font-weight: 400;
				color: #4D4D4D;
				word-break: break-all;
				word-wrap: break-word;
			}

			.itemBtn {
				margin-left: 10rpx;
				display: flex;
				align-items: center;

				.no-danger {
					border-radius: 4px;
					background: rgba(15, 83, 172, 0.2);
					color: #0F53AC;
					line-height: 18px;
					font-size: 12px;
					font-weight: 400;
					display: flex;
					justify-content: center;
					align-items: center;
					padding: 4px 12px 4px 12px;
				}
			}

		}

		.my-num {
			display: flex;
			align-items: center;

			.num {
				width: 18px;
				height: 18px;
				border-radius: 100%;
				background: #FA5151;
				color: #fff;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 24rpx;
				margin-right: 12rpx;
			}
		}

		.signBox {
			display: flex;
			align-items: center;

			.text {
				font-size: 32rpx;
				color: #2B2B2B;
				margin-right: 24rpx;
			}
		}
	}
</style>
